<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/6/16
 * Time: 9:20
 */
namespace Admin\Controller;
use Think\Controller;

class InviteController extends CommonController
{
    protected function _initialize()
    {
        parent::_initialize();
        $this->breadcrumb1 = '财务管理';
        $this->breadcrumb2 = '师傅订单分层管理';
        $this->url = U('Invite/index');
        $this->sqlname='invite'.session('user_auth')['uid'];
    }

    //首页
    public function index(){
        $user=I('post.master_id');
        $user_name=I('post.master_name');
        $year=I('post.year');
        $month=I('post.month');

        if($user){
            $where['u.phone']=array('like',"%$user%");
        }

        if($user_name){
            $where['ifo.id_name']=array('like',"%$user_name%");
        }

        if($month && $year){
            $date1=$year.'-'.$month.'-01';
            $month=$month+1;
            $date2=$year.'-'.$month.'-01';
            $time1=strtotime($date1);
            $time2=strtotime($date2);
            $where['i.add_time']=array('between',array("$time1,$time2"));
        }
        $where['u.is_real']=1;
        $count=M('invite i')
            ->join('left join zsf_user u on u.user_id=i.user_id')
            ->join('left join zsf_user_real_info ifo on ifo.user_id=i.user_id')
            ->field('i.user_id mas_userid,u.phone,ifo.id_name,u.invite,i.add_time')
            //  ->field('i.user_id mas_userid,u.phone,ifo.id_name,u.invite,i.add_time,(select count(*) from zsf_repair_order pr where pr.user_id in(i.share_userid)) people_count,(select sum(total) from zsf_repair_order where user_id in(i.share_userid)) money_count')
            ->where($where)
            ->count();
        $Page       = new \Think\Page($count,25);

        $list=M('invite i')
            ->join('left join zsf_user u on u.user_id=i.user_id')
            ->join('left join zsf_user_real_info ifo on ifo.user_id=i.user_id')
            ->field('i.user_id mas_userid,u.phone,ifo.id_name,u.invite,i.add_time')
          //  ->field('i.user_id mas_userid,u.phone,ifo.id_name,u.invite,i.add_time,(select count(*) from zsf_repair_order pr where pr.user_id in(i.share_userid)) people_count,(select sum(total) from zsf_repair_order where user_id in(i.share_userid)) money_count')
            ->where($where)
            ->select();

        foreach($list as $k=>$v){
            $zwhere['invite_master']=$v['mas_userid'];
            $zwhere['pay_time']=array('gt',0);
           $list[$k]['people_count']= M('repair_order')->where($zwhere)->count();
           $s=M('repair_order')->where($zwhere)->field("sum(total) s")->find();
            $list[$k]['money_count']=$s['s'];
        }
        $redis = S(array('type' => 'redis'));
        $sql=S($this->sqlname,$list);

        $show       = $Page->show();
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->display();
    }


    //详情
    public function detail(){
        $id=I('get.id');
        $type=I('get.type','');
        if($type == 'master'){
            $this->breadcrumb1='师傅管理';
            $this->breadcrumb2='全部订单';
            $this->url = U('Master/index');
        }
        $where['user_id']=$id;
        $list=M('invite i')
            ->where($where)
            ->field('share_userid')
            ->find();
        $rw['r.user_id']=array('in',$list['share_userid']);
        $rw['pay_time']=array('gt',0);
        $count=M('repair_order r')
            ->join('left join zsf_user u on u.user_id=r.user_id')
            ->field('r.repair_order_num_alias,u.phone,u.user_name,r.total,r.set_time')
            ->where($rw)->count();

        $Page       = new \Think\Page($count,25);

        $detailList=M('repair_order r')
            ->join('left join zsf_user u on u.user_id=r.user_id')
            ->field('r.repair_order_num_alias,u.phone,u.user_name,r.total,r.set_time')
            ->where($rw)
            ->limit($Page->firstRow,$Page->listRows)
            ->select();
        $show       = $Page->show();
        $this->assign('list',$detailList);
        $this->assign('page',$show);
        $this->display();
    }


    //excel导出
    public function excelout(){
        $redis = S(array('type' => 'redis'));
        $data=S($this->sqlname);
        //设置列名称
        $sheetindex=array(
            'A'=>array('index'=>'A1','name'=>'序号','size'=>'20'),
            'B'=>array('index'=>'B1','name'=>'师傅账号','size'=>'20'),
            'C'=>array('index'=>'C1','name'=>'师傅名称','size'=>'20'),
            'D'=>array('index'=>'D1','name'=>'邀请码','size'=>'20'),
            'E'=>array('index'=>'E1','name'=>'年月','size'=>'20'),
            'F'=>array('index'=>'F1','name'=>'分享用户订单总量','size'=>'20'),
            'G'=>array('index'=>'G1','name'=>'订单总金额','size'=>'20'),
        );
        //设置标题
        $title='师傅订单分层管理';
        //选择类型
        $type=10;
        //执行sql
        phpExcel($sheetindex,$data,$title,$type);
        exit;
    }
}